home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / NeuroSim 1.0 / .cp / CNeuron.cp < prev    next >
Text File  |  1996-02-19  |  7KB  |  237 lines

  1. // ===========================================================================
  2. //    CNeuron.cp                    ©1996 Timo Eloranta
  3. // ===========================================================================
  4. //  An abstract neuron class - derived from LLink to be queueable
  5.  
  6. #include "CNeuron.h"
  7. #include "CNeuralNet.h"                    // AddToLightQ
  8. #include "NS_Utils.h"                    // PlayOneSnd
  9.  
  10. #include <Icons.h>                        // Universal header
  11. #include <LListIterator.h>                // PowerPlant header
  12.  
  13. CNeuralNet *    CNeuron::sNet = NULL;    // Initialize the static member
  14.  
  15. // ---------------------------------------------------------------------------
  16. //        • SetNeuronState
  17. //
  18. //          Called by:    CNeuron::CNeuron()
  19. //                        CNeuron::IncState
  20. //                        CStdNeuron::DoClickAction
  21. //                        CStdNeuron::SetPostLightUpState
  22. // ---------------------------------------------------------------------------
  23. //    Set the state of the neuron to the given value and mark this neuron
  24. //    as "dirty" so that it will be redrawn when the display is next updated.
  25.  
  26. inline void
  27. CNeuron::SetNeuronState( Uint16 inState )
  28. {
  29.     mState = inState;
  30.     ForceRedraw();
  31. }
  32.  
  33. // ---------------------------------------------------------------------------
  34. //        • CNeuron()
  35. //
  36. //          Called by:    Constructors of subclasses
  37. // ---------------------------------------------------------------------------
  38. //    Default constructor. Can't be called with 'new', since CNeuron is
  39. //    an abstract class.
  40.  
  41. CNeuron::CNeuron()
  42. {
  43.     SetNeuronState( 0 );
  44.     mMaxState        = 0;
  45.     mConnDirty        = true;
  46.     mConnLit        = false;
  47.     mDestination    = false;
  48. }
  49.  
  50. // ---------------------------------------------------------------------------
  51. //        • IncState
  52. //
  53. //          Called by:    CStdNeuron::DoLightUpAction
  54. // ---------------------------------------------------------------------------
  55. //    Increase the state of the neuron by one, unless the state is already
  56. //    high enough to cause the neuron to light up. If the increasing causes
  57. //    the neuron to light up, we add the neuron to the "light up queue".
  58.  
  59. void
  60. CNeuron::IncState()
  61. {
  62.     if ( ! ShouldLightUp() ) {
  63.         SetNeuronState( mState + 1 );
  64.         if ( ShouldLightUp() ) {
  65.             sNet -> AddToLightQ( (LLink *) this );
  66.             PlayOneSnd( snd_Zap, true );
  67.         }
  68.     }
  69. }
  70.  
  71. // ---------------------------------------------------------------------------
  72. //        • SetNeuronPos
  73. //
  74. //          Called by:    CNeuralNet::InitMatrix
  75. // ---------------------------------------------------------------------------
  76. //    Set this neuron's position (column & row) in the matrix.
  77.  
  78. inline void
  79. CNeuron::SetNeuronPos( Uint16 inCol, Uint16 inRow )
  80. {
  81.     mCol = inCol;
  82.     mRow = inRow;
  83. }
  84.  
  85. // ---------------------------------------------------------------------------
  86. //        • GetNeuronPos
  87. //
  88. //          Called by:    CNeuralNet::GenerateConnections
  89. // ---------------------------------------------------------------------------
  90. //    Get this neuron's position (column & row) in the matrix.
  91.  
  92. inline void
  93. CNeuron::GetNeuronPos( Uint16 & outCol, Uint16 & outRow) const
  94. {
  95.     outCol = mCol; 
  96.     outRow = mRow;
  97. }
  98.  
  99. // ---------------------------------------------------------------------------
  100. //        • IsReceptor
  101. //
  102. //          Called by:    CStdNeuron::DoClickAction
  103. // ---------------------------------------------------------------------------
  104. //    A neuron is a receptor if it can be manually lit by the user.
  105. //    By default the neurons in the first column are receptors.
  106.  
  107. inline Boolean
  108. CNeuron::IsReceptor() const
  109. {
  110.     return mCol == 1;
  111. }
  112.  
  113. // ---------------------------------------------------------------------------
  114. //        • AddToNeuronList
  115. //
  116. //          Called by:    CNeuralNet::GenerateConnections
  117. // ---------------------------------------------------------------------------
  118. //    Add the given neuron to the end of this neurons list of connected
  119. //    neurons. Also update the mMaxState attribute!
  120.  
  121. inline void
  122. CNeuron::AddToNeuronList( const CNeuronPtr inNeuronPtr )
  123. {
  124.     mNextOnes.InsertItemsAt( 1, arrayIndex_Last, &inNeuronPtr );
  125.     mMaxState++;
  126. }
  127.  
  128. // ---------------------------------------------------------------------------
  129. //        • IsConnectedTo
  130. //
  131. //          Called by:    CNeuralNet::GenerateConnections
  132. // ---------------------------------------------------------------------------
  133. //    Return true if this neuron has a connection to the neuron which
  134. //    inNeuronPtr points to. Otherwise return false.
  135.  
  136. Boolean
  137. CNeuron::IsConnectedTo( const CNeuronPtr inNeuronPtr )
  138. {
  139.     LListIterator    theScanner( mNextOnes, iterate_FromStart );
  140.     CNeuronPtr        theNeuron;
  141.     
  142.     while ( theScanner.Next( &theNeuron )) {
  143.         if ( theNeuron == inNeuronPtr )
  144.             return true;
  145.     }
  146.     
  147.     return false;
  148. }
  149.  
  150. // ---------------------------------------------------------------------------
  151. //        • SetNet
  152. //
  153. //          Called by:    CNeuralNet::InitMatrix
  154. // ---------------------------------------------------------------------------
  155. //    Set the value of the static member sNet which points to the 
  156. //    CNeuralNet object which owns this and all the other neurons.
  157.  
  158. void
  159. CNeuron::SetNet( CNeuralNet *inNet )
  160. {
  161.     sNet = inNet;
  162. }
  163.  
  164. // ---------------------------------------------------------------------------
  165. //        • Draw
  166. //
  167. //          Called by:    CNeuralNet::DrawNeurons
  168. // ---------------------------------------------------------------------------
  169. //    Draw this neuron (without the connections!) with a number and a color
  170. //    which show the state of the neuron. If the neuron is below its maximum 
  171. //    state its color is either green (if state = 0) or blue (state > 0).
  172. //    If the neuron is in its maximum state, it's color is red. Yellow is the 
  173. //    color of a neuron which is over its maximum state (when lit up).
  174. //    The icon resources are numbered as follows:
  175. //            green: 200,  blue: 201-208,  red: 300-309,  yellow: 400
  176.  
  177. void 
  178. CNeuron::Draw( const Rect & inRect )
  179. {
  180.     Int16    theResID;                // ID of the icon resource
  181.     
  182.     if ( mState < mMaxState )
  183.         theResID = 200 + mState;
  184.     else if    ( mState == mMaxState )
  185.         theResID = 300 + mState;
  186.     else                            // mState > mMaxState 
  187.         theResID = 400;                // (= the neuron is lit up)
  188.     
  189.     ::PlotIconID(    &inRect,        // Rect
  190.                     atNone,            // AlignmentType
  191.                     ttNone,            // IconTransformType
  192.                     theResID);        // Icon resource ID
  193.  
  194.     // The following has to do with the case where the user checks
  195.     // which neurons are connected to a certain neuron by clicking
  196.     // a "normal" neuron or option-clicking a receptor. We draw a
  197.     // yellow ring around the connected neurons, because simply
  198.     // lighting the connections isn't enough in the case of multiple
  199.     // connected neurons being in the exact same direction...
  200.     // The resource ID of the "yellow ring" icon is 500.
  201.     
  202.     if ( mDestination )
  203.         ::PlotIconID( &inRect, atNone, ttNone, 500 );
  204.                     
  205.     mRedrawNeeded = false;            // Just drew it, so it's fine now
  206. }
  207.  
  208. // ---------------------------------------------------------------------------
  209. //        • DrawConnections
  210. //
  211. //          Called by:    CNeuralNet::DrawConnections
  212. // ---------------------------------------------------------------------------
  213. //    Draw the connections of this neuron by going through the mNextOnes list.
  214. //    If the connections are lit up, they are drawn in yellow color, otherwise
  215. //    in the color set by CNeuroSimPane::DrawConnections().
  216.  
  217. void 
  218. CNeuron::DrawConnections( )
  219. {
  220.     LListIterator    theScanner( mNextOnes, iterate_FromStart );
  221.     CNeuronPtr        theNeuron;
  222.     const RGBColor  kYellowRGB = { 0xFC00, 0xF37D, 0x052F };        
  223.  
  224.     if ( mConnLit )
  225.         ::RGBForeColor( &kYellowRGB );
  226.  
  227.     while ( theScanner.Next( &theNeuron )) {
  228.  
  229.         ::MoveTo(    mCenter.h, mCenter.v );
  230.         
  231.         ::LineTo(    theNeuron -> mCenter.h, 
  232.                     theNeuron -> mCenter.v );
  233.     }
  234.     
  235.     mConnDirty = false;        // Just drew 'em, so they are "clean" now...
  236. }
  237.